//
//  WPLoopView.m
//  wobingwoyi
//
//  Created by WGP on 16/7/26.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPLoopView.h"
#import "WPCollectionViewCell.h"
#import "WPCollectioViewFlowLayout.h"

@interface WPLoopView ()<UICollectionViewDataSource,UICollectionViewDelegate>


// 分页指示器
@property (nonatomic, strong) UIPageControl *pageControl;
// 录播器
@property (nonatomic, strong) UICollectionView *collectionView;
// 标题数组
@property (nonatomic, strong) NSArray *titles;
// 定时器
@property (nonatomic, strong) NSTimer *timer;
// 选中回调block
@property (nonatomic, copy) void (^didSelected)(NSInteger index);

@end

static NSString *itemID = @"loopCell";
@implementation WPLoopView

/**
 *  创建图片轮播器
 *
 *  @param URLStrs 图片URL数组
 *  @param titles  标题数组
 */
- (instancetype)initWithURLStrs:(NSArray <NSString *> *)URLStrs titles:(NSArray <NSString *> *)titles didSelected:(void (^)(NSInteger index))didSelected{
    if (self = [super init]) {
        
        // 记录属性
        self.URLStrs = URLStrs;
        self.titles = titles;
        self.didSelected = didSelected;
        
        // 设置页数
        self.pageControl.numberOfPages = URLStrs.count;
        
        // 滚动到 URLStrs.count 位置 3
        // 主队列异步执行：在主线程空闲时才能执行block代码
        // 在执行block代码时，已经调用了collectionView数据源方法
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (self.URLStrs.count > 1) {
                [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:URLStrs.count inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
                // 添加定时器
                [self addTimer];
            }
        });
    }
    return self;
}

-(void)setURLStrs:(NSArray *)URLStrs
{
    _URLStrs = URLStrs;
    
    // 设置页数
    self.pageControl.numberOfPages = URLStrs.count;
    
    // 滚动到 URLStrs.count 位置 3
    // 主队列异步执行：在主线程空闲时才能执行block代码
    // 在执行block代码时，已经调用了collectionView数据源方法
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (self.URLStrs.count > 1) {
            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:URLStrs.count inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
            // 添加定时器
            [self addTimer];
        }
    });
    [self.collectionView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self= [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}

- (void)setup {
    
    // 创建CollectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[WPCollectioViewFlowLayout alloc] init]];
    // 注册cell
    [collectionView registerClass:[WPCollectionViewCell class] forCellWithReuseIdentifier:itemID];
    // 设置代理和数据源
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    collectionView.backgroundColor = RGB_SAME(245);
    
    
    [self addSubview:collectionView];
    self.collectionView = collectionView;
    
    // 分页指示器
    self.pageControl = [[UIPageControl alloc] init];
    self.pageControl.currentPageIndicatorTintColor = GlobalColor;
    self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    // 如果只有一页 则隐藏分页指示器
    self.pageControl.hidesForSinglePage = YES;
    [self addSubview:self.pageControl];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(10);
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-10);
    }];
    
    // 设置默认时间 默认值2
    self.timerInterval = 3;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.collectionView.frame = self.bounds;
}

#pragma mark - 定时器相关方法
/**
 *  创建定时器
 */
-(void)addTimer {
    self.timer =  [NSTimer scheduledTimerWithTimeInterval:self.timerInterval target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
/**
 *  移除定时器
 */
- (void)removeTimer {
    [self.timer invalidate];
    self.timer = nil;
}

/**
 *  定时器回调方法
 */
- (void)nextImage {
    // 获得当前显示的页号
    NSInteger page = self.collectionView.contentOffset.x / self.collectionView.bounds.size.width;
    // 计算偏移量
    CGFloat offsetX = (page + 1) * self.collectionView.bounds.size.width;
    // 设置偏移量
    [self.collectionView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

#pragma mark - UICollectionView数据源方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.URLStrs.count * ((self.URLStrs.count == 1) ? 1 : 2);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WPCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:itemID forIndexPath:indexPath];
    cell.URLString = self.URLStrs[indexPath.item%self.URLStrs.count];
    return cell;
}

/**
 *  监听item的点击
 */
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.didSelected) {
        
        self.didSelected(indexPath.item % self.URLStrs.count);
    }
}

#pragma mark - UIScrollView 代理方法
/**
 *  当用户开始拖拽时调用
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self removeTimer];
}

/**
 *  当用户结束拖拽时调用
 */
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    [self addTimer];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self scrollViewDidEndDecelerating:scrollView];
}

/**
 *  手动拖拽时才会触发
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 计算当前滚动的页号
    NSInteger page = scrollView.contentOffset.x / scrollView.bounds.size.width;
    
    if (page == 0 || page == ([self.collectionView numberOfItemsInSection:0] - 1)) {
        
        page = self.URLStrs.count - ((page == 0) ? 0: 1); // 3
        // 滚动到第URLStrs.count张
        self.collectionView.contentOffset = CGPointMake(page * scrollView.bounds.size.width, 0);
    }
    self.pageControl.currentPage = page % self.URLStrs.count;
}

- (void)dealloc {
    [self removeTimer];
    
}


@end









